//
//  AddAccountViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/7.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "AddAccountViewController.h"
#import "AccountNameViewController.h"
@interface AddAccountViewController ()
{
    cellData *_selectData;
}
@property(nonatomic,strong)ykTableView *tableView;
@property(nonatomic,strong)UIView *chooseView;
@end

@implementation AddAccountViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加账户";
    
    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
    leftButton.selected=YES;
    
    self.tableView =[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT+10, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT-10)  withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        
        
        if (indexPath.section==0&& indexPath.row==0)
            {
                if (self.chooseView==nil)
                {
                    NSArray *titleArr=[[[NSArray arrayWithObjects:@"门店",@"电动车厂商",@"民警",@"物业", nil] reverseObjectEnumerator] allObjects];
                
                


                    self.chooseView=[[UIView alloc]init];
                    self.chooseView.backgroundColor = [UIColor colorWithRed:0/255. green:0/255. blue:0/255. alpha:0.1];
                    [self.view.window addSubview:self.chooseView];
                    [self.chooseView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.center.mas_equalTo(self.view.window);
                        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
                    }];



                    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideCurrentVC:)];
                    [self.chooseView addGestureRecognizer:tap];




                    for (int i=0; i<titleArr.count; i++) {
                        //角色按钮
                        UIButton *itemBtn=[SingLeton endButton:[UIColor whiteColor] andTitle:titleArr[i] RequestSuccess:^(UIButton *btn) {
                            [self.chooseView removeFromSuperview];
                            self.chooseView=nil;
                            //系统cell
                            [self.tableView.dataArray removeAllObjects];
                            NSString *title=btn.titleLabel.text;
                            
                            if ([title isEqualToString:@"管理员"]) {
                             
                                
                                
                                NSMutableArray *itemArr=[NSMutableArray array];
                                NSMutableArray *itemArr2=[NSMutableArray array];
                                cellData *dataObj=[[cellData alloc]init];
                                dataObj.text=@"角色类型";
                                dataObj.detailText=title;
                                
                                
                                
                                [itemArr addObject:dataObj];
                                
                                
                                cellData *dataObj3=[[cellData alloc]init];
                                dataObj3.text=@"账号";
                                dataObj3.detailText=@"13800000000";
                                
                                
                                cellData *dataObj4=[[cellData alloc]init];
                                dataObj4.text=@"账户名";
                                dataObj4.detailText=@"";
                                
                                [itemArr2 addObject:dataObj3];
                                [itemArr2 addObject:dataObj4];
                                
                                [self.tableView.dataArray addObject:itemArr];
                                [self.tableView.dataArray addObject:itemArr2];
                                [self.tableView reloadData];
                                
                                
                                
                    self.tableView.tableFooterView=[self customFootView];
                               
                                
                                
                                
                                
                            }
                            else if ([title isEqualToString:@"门店"])
                            {
                                NSMutableArray *itemArr=[NSMutableArray array];
                                NSMutableArray *itemArr2=[NSMutableArray array];
                                cellData *dataObj=[[cellData alloc]init];
                                dataObj.text=@"角色类型";
                                dataObj.detailText=title;
                                
                                
                                
                                cellData *dataObj3=[[cellData alloc]init];
                                dataObj3.text=@"账号";
                                dataObj3.detailText=@"13800000000";
                                
                                
                                [itemArr addObject:dataObj];
                                
                                [itemArr addObject:dataObj3];
                                
                                cellData *dataObj4=[[cellData alloc]init];
                                dataObj4.text=@"门店名称";
                                dataObj4.detailText=@"门店名称";
                                
                                
                                cellData *dataObj5=[[cellData alloc]init];
                                dataObj5.text=@"联系人";
                                dataObj5.detailText=@"某某某";
                                
                                cellData *dataObj6=[[cellData alloc]init];
                                dataObj6.text=@"联系电话";
                                dataObj6.detailText=@"13800000000";
                                
                                cellData *dataObj7=[[cellData alloc]init];
                                dataObj7.text=@"地址";
                                dataObj7.detailText=@"我家在东北松花江上啊我家在东北松花江上啊";
                                
                                
                                [itemArr2 addObject:dataObj4];
                                [itemArr2 addObject:dataObj5];
                                [itemArr2 addObject:dataObj6];
                                [itemArr2 addObject:dataObj7];
                                
                                [self.tableView.dataArray addObject:itemArr];
                                [self.tableView.dataArray addObject:itemArr2];
                                [self.tableView reloadData];
                                
                                
                                
                                self.tableView.tableFooterView=[self customFootView];
                                
                            }
                            else if ([title isEqualToString:@"修理商"])
                            {
                                
                                NSMutableArray *itemArr=[NSMutableArray array];
                                NSMutableArray *itemArr2=[NSMutableArray array];
                                cellData *dataObj=[[cellData alloc]init];
                                dataObj.text=@"角色类型";
                                dataObj.detailText=title;
                                
                                
                                
                                cellData *dataObj3=[[cellData alloc]init];
                                dataObj3.text=@"账号";
                                dataObj3.detailText=@"13800000000";
                                
                                
                                [itemArr addObject:dataObj];
                                
                                [itemArr addObject:dataObj3];
                                
                                cellData *dataObj4=[[cellData alloc]init];
                                dataObj4.text=@"账户名";
                                dataObj4.detailText=@"修理商名称";
                                
                                
                                cellData *dataObj5=[[cellData alloc]init];
                                dataObj5.text=@"联系人";
                                dataObj5.detailText=@"某某某";
                                
                                cellData *dataObj6=[[cellData alloc]init];
                                dataObj6.text=@"联系电话";
                                dataObj6.detailText=@"13800000000";
                                
                                cellData *dataObj7=[[cellData alloc]init];
                                dataObj7.text=@"地址";
                                dataObj7.detailText=@"我家在东北松花江上啊我家在东北松花江上啊";
                                
                                
                                [itemArr2 addObject:dataObj4];
                                [itemArr2 addObject:dataObj5];
                                [itemArr2 addObject:dataObj6];
                                [itemArr2 addObject:dataObj7];
                                
                                [self.tableView.dataArray addObject:itemArr];
                                [self.tableView.dataArray addObject:itemArr2];
                                [self.tableView reloadData];
                                
                                
                                
                                self.tableView.tableFooterView=[self customFootView];
                            }
                            else if ([title isEqualToString:@"电动车厂商"])
                            {
                                
                                NSMutableArray *itemArr=[NSMutableArray array];
                                NSMutableArray *itemArr2=[NSMutableArray array];
                                NSMutableArray *itemArr3=[NSMutableArray array];
                                
                                
                                cellData *dataObj=[[cellData alloc]init];
                                dataObj.text=@"角色类型";
                                dataObj.detailText=title;
                                
                                
                                
                                cellData *dataObj3=[[cellData alloc]init];
                                dataObj3.text=@"账号";
                                dataObj3.detailText=@"13800000000";
                                
                                [itemArr addObject:dataObj];
                                
                                [itemArr addObject:dataObj3];
                                
                                
                                
                                
                                cellData *dataObj4=[[cellData alloc]init];
                                dataObj4.text=@"电动车品牌";
                                dataObj4.detailText=@"电动车品牌";
                                
                                cellData *dataObj5=[[cellData alloc]init];
                                dataObj5.text=@"联系人";
                                dataObj5.detailText=@"某某某";
                                
                                cellData *dataObj6=[[cellData alloc]init];
                                dataObj6.text=@"联系电话";
                                dataObj6.detailText=@"13800000000";
                                
                                
                                cellData *dataObj7=[[cellData alloc]init];
                                dataObj7.text=@"地址";
                                dataObj7.detailText=@"";
                                
                                [itemArr2 addObject:dataObj4];
                                [itemArr2 addObject:dataObj5];
                                [itemArr2 addObject:dataObj6];
                                [itemArr2 addObject:dataObj7];
                                
                                
                                cellData *dataObj8=[[cellData alloc]init];
                                dataObj8.text=@"电动车品牌";
                                dataObj8.detailText=@"填写品牌名称";
                                
                                [itemArr3 addObject:dataObj8];
                                
                                [self.tableView.dataArray addObject:itemArr];
                                [self.tableView.dataArray addObject:itemArr2];
//                                [self.tableView.dataArray addObject:itemArr3];
                                
                                
                                
                                
                                [self.tableView reloadData];
                            self.tableView.tableFooterView=[self customFootView];
                            }
                            else if ([title isEqualToString:@"民警"])
                            {
                                NSMutableArray *itemArr=[NSMutableArray array];
                                NSMutableArray *itemArr2=[NSMutableArray array];
                                
                                
                                cellData *dataObj=[[cellData alloc]init];
                                dataObj.text=@"角色类型";
                                dataObj.detailText=title;
                                
                                
                                
                                cellData *dataObj3=[[cellData alloc]init];
                                dataObj3.text=@"账号";
                                dataObj3.detailText=@"13800000000";
                                
                                [itemArr addObject:dataObj];
                                
                                [itemArr addObject:dataObj3];
                                
                                
                                
                                
                                cellData *dataObj4=[[cellData alloc]init];
                                dataObj4.text=@"账户名";
                                dataObj4.detailText=@"";
                                
                                
                                
                                cellData *dataObj6=[[cellData alloc]init];
                                dataObj6.text=@"联系电话";
                                dataObj6.detailText=@"";
                                
                                
                                cellData *dataObj7=[[cellData alloc]init];
                                dataObj7.text=@"省市区";
                                dataObj7.detailText=@"选择地区";
                                
                                [itemArr2 addObject:dataObj4];
                                
                                [itemArr2 addObject:dataObj6];
//                                [itemArr2 addObject:dataObj7];
                                
                                [self.tableView.dataArray addObject:itemArr];
                                [self.tableView.dataArray addObject:itemArr2];
                                
                                
                                [self.tableView reloadData];
                                self.tableView.tableFooterView=[self customFootView];
                            }
                            else if ([title isEqualToString:@"物业"])
                            {
                                
                                NSMutableArray *itemArr=[NSMutableArray array];
                                NSMutableArray *itemArr2=[NSMutableArray array];
                                
                                
                                cellData *dataObj=[[cellData alloc]init];
                                dataObj.text=@"角色类型";
                                dataObj.detailText=title;
                                
                                
                                
                                cellData *dataObj3=[[cellData alloc]init];
                                dataObj3.text=@"账号";
                                dataObj3.detailText=@"13800000000";
                                
                                [itemArr addObject:dataObj];
                                
                                [itemArr addObject:dataObj3];
                                
                                
                                
                                
                                cellData *dataObj4=[[cellData alloc]init];
                                dataObj4.text=@"账户名";
                                dataObj4.detailText=@"";
                                
                                cellData *dataObj5=[[cellData alloc]init];
                                dataObj5.text=@"联系人";
                                dataObj5.detailText=@"某某某";
                                
                                cellData *dataObj6=[[cellData alloc]init];
                                dataObj6.text=@"联系电话";
                                dataObj6.detailText=@"";
                                
                                
                                
                                
                                [itemArr2 addObject:dataObj4];
                                [itemArr2 addObject:dataObj5];
                                [itemArr2 addObject:dataObj6];
                                
                                
                                [self.tableView.dataArray addObject:itemArr];
                                [self.tableView.dataArray addObject:itemArr2];
                                
                                [self.tableView reloadData];
                                self.tableView.tableFooterView=[self customFootView];
                            }

                        }];
                        [self.chooseView addSubview:itemBtn];
                        [itemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.left.mas_equalTo(0);
                            make.bottom.mas_equalTo(-44.5*i);
                            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 44));
                        }];
                        [itemBtn setTitleColor:[UIColor color3] forState:UIControlStateNormal];
                        itemBtn.titleLabel.font=[UIFont systemFontOfSize:14.0];
                        itemBtn.layer.cornerRadius=0;
                    }
                }
                
  
            }
        UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        NSArray *sectionArr=self.tableView.dataArray[indexPath.section];
        
        _selectData=sectionArr[indexPath.row];
        if ([cell.textLabel.text isEqualToString:@"账户名"]||[cell.textLabel.text isEqualToString:@"账号"]||[cell.textLabel.text isEqualToString:@"联系人"]||[cell.textLabel.text isEqualToString:@"联系电话"]||[cell.textLabel.text isEqualToString:@"地址"]||[cell.textLabel.text isEqualToString:@"门店名称"]||[cell.textLabel.text isEqualToString:@"电动车品牌"]) {
            AccountNameViewController *vc=[[AccountNameViewController alloc]init];
            vc.type=cell.textLabel.text;
            vc.content=cell.detailTextLabel.text;
            [self.navigationController pushViewController:vc animated:YES];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getReturnInfo:) name:@"填写回调" object:nil];
        }
        
        
        

        
        
    }];
    [self.view addSubview:self.tableView];
    
    
    
    
    //系统cell
    [self.tableView.dataArray removeAllObjects];
    NSMutableArray *itemArr=[NSMutableArray array];
    cellData *dataObj=[[cellData alloc]init];
    dataObj.image=@"默认头像";
    dataObj.text=@"角色类型";
    dataObj.detailText=@"选择类型";
    dataObj.numText=@"101010100001";
    [itemArr addObject:dataObj];
    [self.tableView.dataArray addObject:itemArr];
    [self.tableView reloadData];
    
    
    
}
-(void)getReturnInfo:(NSNotification *)notic
{
    
    
    _selectData.detailText=notic.userInfo[@"backinfo"];
    [self.tableView reloadData];
    
    
    
    
}
-(void)hideCurrentVC:(UIGestureRecognizer *)tap{
    [self.chooseView removeFromSuperview];
    self.chooseView=nil;
}
-(UIView *)customFootView
{
    UIView *footView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    
    
    UIButton *saveBtn=[SingLeton endButton:[UIColor colorQ] andTitle:@"保存" RequestSuccess:^(UIButton *btn) {
        
        
        
        NSString *URLString = @"/axq/admin/save";
        /*
         1 uid
         2 loginid  登录手机号
         4 userrole 角色（+)
         5 usertitle 账户名称
         6 contact 联系人
         7 address 地址
         8 telephone 联系电话
         9 headimg 头像url
         
         */
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        for (int i=0; i<self.tableView.dataArray.count; i++) {
            if ([self.tableView.dataArray[i] isKindOfClass:[NSArray class]]) {
                NSArray *sectionArr=self.tableView.dataArray[i];
                for (int i=0; i<sectionArr.count; i++) {
                    cellData *dataObj=sectionArr[i];
                    if ([dataObj.text isEqualToString:@"角色类型"]) {
                        if ([dataObj.detailText isEqualToString:@"管理员"]) {
                            [dic setObject:@"admin" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"门店"])
                        {
                            [dic setObject:@"shop" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"修理商"])
                        {
                            [dic setObject:@"repair" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"电动车厂商"])
                        {
                            [dic setObject:@"producer" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"民警"])
                        {
                            [dic setObject:@"police" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"物业"])
                        {
                            [dic setObject:@"property" forKey:@"userrole"];
                        }
                    }
                    else if ([dataObj.text isEqualToString:@"账号"])
                    {
                      [dic setObject:dataObj.detailText forKey:@"loginid"];
                    }
                    else if ([dataObj.text isEqualToString:@"账户名"])
                    {
                       [dic setObject:dataObj.detailText forKey:@"usertitle"];
                    }
                    else if ([dataObj.text isEqualToString:@"联系人"])
                    {
                      [dic setObject:dataObj.detailText forKey:@"contact"];
                    }
                    else if ([dataObj.text isEqualToString:@"联系电话"])
                    {
                      [dic setObject:dataObj.detailText forKey:@"telephone"];
                    }
                    else if ([dataObj.text isEqualToString:@"地址"])
                    {
                      [dic setObject:dataObj.detailText forKey:@"address"];
                    }
                    else if ([dataObj.text isEqualToString:@"门店名称"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"usertitle"];
                    }
                    else if ([dataObj.text isEqualToString:@"电动车品牌"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"usertitle"];
                    }
                    
                }
            }else
            {
              cellData *dataObj=self.tableView.dataArray[i];
                if ([dataObj.text isEqualToString:@"角色类型"]) {
                    
                    if ([dataObj.detailText isEqualToString:@"管理员"]) {
                        [dic setObject:@"admin" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"门店"])
                    {
                        [dic setObject:@"shop" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"修理商"])
                    {
                        [dic setObject:@"repair" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"电动车厂商"])
                    {
                        [dic setObject:@"producer" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"民警"])
                    {
                        [dic setObject:@"police" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"物业"])
                    {
                        [dic setObject:@"property" forKey:@"userrole"];
                    }
                }
                else if ([dataObj.text isEqualToString:@"账号"])
                {
                    [dic setObject:dataObj.detailText forKey:@"loginid"];
                }
                else if ([dataObj.text isEqualToString:@"账户名"])
                {
                    [dic setObject:dataObj.detailText forKey:@"usertitle"];
                }
                else if ([dataObj.text isEqualToString:@"联系人"])
                {
                    [dic setObject:dataObj.detailText forKey:@"contact"];
                }
                else if ([dataObj.text isEqualToString:@"联系电话"])
                {
                    [dic setObject:dataObj.detailText forKey:@"telephone"];
                }
                else if ([dataObj.text isEqualToString:@"地址"])
                {
                    [dic setObject:dataObj.detailText forKey:@"address"];
                }
                else if ([dataObj.text isEqualToString:@"门店名称"])
                {
                    [dic setObject:dataObj.detailText forKey:@"usertitle"];
                }
                else if ([dataObj.text isEqualToString:@"电动车品牌"])
                {
                    [dic setObject:dataObj.detailText forKey:@"usertitle"];
                }
            }
        }
        
        [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:dic RequestSuccess:^(NSURLResponse *response, id responseObject) {
            NSLog(@"%@",responseObject);
            NSString *code = responseObject[@"code"];
            if([code isEqualToString:@"10008"]){
//                NSDictionary *data=responseObject[@"data"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                NSString *str=responseObject[@"msg"];
                
                [self.view makeToast:str];
            }
        } RequestFailed:^(NSError *error) {
            
        }];
    }];
    [footView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-30, 44));
    }];
    
    
    UILabel *tipLab=[SingLeton myLabel:[UIColor colorWithHexString:@"ff556d"] andTitle:@"账户初始密码：123456" andFont:13.0];
    [footView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(footView);
        make.top.mas_equalTo(saveBtn.mas_bottom).mas_offset(10);
        
    }];
    return footView;
}
@end
